<?php

namespace App\Console\Commands;

use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductModelNumber;
use Illuminate\Console\Command;

class UpModelnumberSpecification extends Command
{
    /**
     * The name and signature of the console command.
     * php artisan command:upmodelnumberspecification
     * @var string
     */
    protected $signature = 'command:upmodelnumberspecification';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

//        $specification_id_arr = [1,2,3,4];
//        $product = Product::where('modelid',2)->get()->toArray();
//        foreach($product as $k=>$v){
//            ModelNumberSpecification::where('product_id',$v['id'])->delete();
//            $model_number_id_arr = ProductModelNumber::where('product_id',$v['id'])->pluck('model_number_id')->toArray();
//            if(!empty($model_number_id_arr)){
//                foreach($model_number_id_arr as $kk=>$vv){
//                    foreach($specification_id_arr as $val){
//                        $this->info('产品：'.$v['product_name'].'型号：'.$vv.';规格：'.$val);
//                        ModelNumberSpecification::updateOrCreate(
//                            [
//                                'product_id'=>$v['id'],
//                                'model_number_id'=>$vv,
//                                'specification_id'=>$val
//                            ],[
//                                'product_id'=>$v['id'],
//                                'model_number_id'=>$vv,
//                                'specification_id'=>$val
//                            ]
//                        );
//                    }
//                }
//
//            }
//        }

//        for($i = 57;$i<=104;$i++){
//            $specification_id_arr[] = $i;
//        }
//        $product = Product::where('modelid',1)->where('able_level',1)->get()->toArray();

        $specification_id_arr = [27,43,44,45];
        $product = Product::where('modelid',1)->where('able_level',2)->get()->toArray();

        foreach($product as $k=>$v){
            $model_number_id_arr = ProductModelNumber::where('product_id',$v['id'])->pluck('model_number_id')->toArray();
            if(!empty($model_number_id_arr)){
                foreach($model_number_id_arr as $kk=>$vv){
                    foreach($specification_id_arr as $val){
                        $this->info('产品：'.$v['product_name'].'型号：'.$vv.';规格：'.$val);
                        ModelNumberSpecification::updateOrCreate(
                            [
                                'product_id'=>$v['id'],
                                'model_number_id'=>$vv,
                                'specification_id'=>$val
                            ],[
                                'product_id'=>$v['id'],
                                'model_number_id'=>$vv,
                                'specification_id'=>$val
                            ]
                        );
                    }
                }

            }
        }
        return Command::SUCCESS;
    }
}
